

from tkinter import *


root = Tk()



text = Text(root,width=50,height=10)

text.pack()

text.insert(INSERT,"i love hoho,and you?")

def show():
    text.image_create(END,image=photo)

b1 = Button(text,text="我是按钮",command=show)

# ######################
# text 对象可以插入按钮，图片
# ######################
text.window_create(INSERT,window=b1)

photo = PhotoImage(file="../mm/005TgvqCgy1ftv5mu09uhg30bo06knph.gif")


# ######################
# Mark,插入东西会在标记的右边
# ######################

text.mark_set("here","1.2")
# text.mark_gravity("here",LEFT)
text.insert("here","1")
text.insert("here","2")


# ######################
# Tags
# ######################

# 设置tag
text.tag_add("tag1","1.5","1.9","1.12")
text.tag_add("tag2","1.5","1.9","1.12")

# 配置 tag 样式
text.tag_config("tag1",background="#66DC47",foreground="#fff")
text.tag_config("tag2",foreground="blue")

text.insert(INSERT,"fishc.com",("tag2","tag1"))


import webbrowser

def show(event):
    print(1)
    text.config(cursor="arrow")

def hide(event):
    print(2)
    text.config("tag2",cursor="xterm")

def click(event):
    print(3)
    webbrowser.open("http://hoholoving.com")

text.tag_bind("tag2","<Enter>",show)
text.tag_bind("tag2","<Leave>",hide)
text.tag_bind("tag2","<Button-1>",click)


# 使用技巧
import hashlib

# 验证 MD5 来看内容是否变化
content = text.get("1.0",END)

def getSig(content):
    n = hashlib.md5(content.encode())

    return n.digest()

sign = getSig(content)

def check():
    content = text.get("1.0",END)

    if sign!= getSig(content):
        print("内容不一样")

Button(root,text="检测内容",command=check).pack()


# 查找操作

start="1.0"

def getIndex(text,index):
    return tuple(map(int,str.split(text.index(index),".")))

while True:
    pos=text.search("o",start,stopindex=END)
    if not pos:
        break;
    print("找到了",getIndex(text,pos))
    start = pos+ "+1c"




# 撤销

# 开启 undo，每次操作都会有记录
# 开启 autoseparator，自己设置分隔符
text2 = Text(root,width=30,height=10,undo=True,autoseparator=False)

text2.pack()

text2.insert(INSERT,"I love hoholoving.com")

def cancle():
    text2.edit_undo()

# 自己设置撤回分隔符:就是一次性撤回多少
def callback(event):
    text2.edit_separator()

text2.bind("<Key>",callback)

Button(root,text="撤销",command=cancle).pack()

# 没有东西撤销的时候，还要撤销会抛出异常






















mainloop()